package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.Shelter;
import DBTasks.ShelterSearch;

@SuppressWarnings("serial")
public class ShelterResultsPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model;
	private JTable table;
	private JTableHeader headers;
	private MainFrame frame;

	public ShelterResultsPanel(MainFrame mainFrame, int zipInput) {
		
		//this.setSize(800, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		JLabel shelterResults = new JLabel("Shelter Search Results for Zip Code " + zipInput);
		shelterResults.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] columnNames = {"Name", "Address", "Phone", "Days Open", "Hours Open"};
		List<Shelter> shelters = ShelterSearch.search(zipInput);
		Object[][] data = new Object[shelters.size()][5];
		for(int i = 0; i < data.length; i++) {
			data[i][0] = shelters.get(i).getName();
			data[i][1] = shelters.get(i).getAddress();
			data[i][2] = shelters.get(i).getPhoneNumber();
			data[i][3] = shelters.get(i).getDaysOpen();
			data[i][4] = shelters.get(i).getHoursOpen();
		}
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		table.setGridColor(Color.black);
		table.setRowHeight(30);
		headers = table.getTableHeader();
		table.setBorder(BorderFactory.createLineBorder(Color.black));
		headers.setBorder(BorderFactory.createLineBorder(Color.black));
		
		table.getColumnModel().getColumn(0).setPreferredWidth(150);
		table.getColumnModel().getColumn(1).setPreferredWidth(250);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);

        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(shelterResults)
				.addComponent(headers)
				.addComponent(table)
				.addGroup(gl.createSequentialGroup()
						.addGap(575)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(shelterResults)
			    .addGap(20)
			    .addComponent(headers)
			    .addComponent(table)
			    .addGap(20)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getSearch());
		}
	}

}
